package com.csproj.system.repository;


import com.csproj.system.domain.RoleInfo;
import com.csproj.system.repository.custom.RoleInfoRepositoryCustom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Spring Data JPA repository for the RoleInfo entity.
 */
@SuppressWarnings("unused")
public interface RoleInfoRepository extends JpaRepository<RoleInfo,String>, RoleInfoRepositoryCustom {

    @Query("select distinct roleInfo from RoleInfo roleInfo left join fetch roleInfo.authorityInfos where roleInfo.deletedFlag = 0 ")
    List<RoleInfo> findAllWithEagerRelationships();

    @Query("select roleInfo from RoleInfo roleInfo left join fetch roleInfo.authorityInfos where roleInfo.id =:id and roleInfo.deletedFlag = 0 ")
    RoleInfo findOneWithEagerRelationships(@Param("id") String id);


    /**
     * 根据id查询角色
     *
     * @return
     */
    @Query("select ri from RoleInfo ri where  ri.id = ?1   ")
    RoleInfo findOneAuthNotNull(String id);


    /**
     * 查询所有未删除的角色信息
     * @return
     */
    @Query(" from RoleInfo ri where ri.deletedFlag = 0 ")
    List<RoleInfo> findAll();

    /**
     * 删除角色信息
     * @param id
     */
    @Modifying
    @Query(" update RoleInfo ri SET ri.deletedFlag = 1 where ri.id = ?1")
    void delete(String id);

    /**
     * 根据用户ID获取用户的角色
     * @param userInfoId
     * @return
     */
    @Query("select ri from RoleInfo ri left join ri.userInfos ui where ri.deletedFlag = 0 and ui.id = ?1")
    List<RoleInfo> findByUserInfoId(String userInfoId);


    /**
     * find list By id array
     * @return
     */
    @Query("select ri from RoleInfo  ri where id in (?1)" )
    List<RoleInfo> findByIds(String[] ids);

    /**
     * findByName
     * @param name
     * @return
     */
    @Query(" from RoleInfo  ri where name = ?1 and deletedFlag = 0 ")
    RoleInfo findByName(String name);
}
